package com.project2.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketGoods;
import com.project2.bean.po.MarketTopic;
import com.project2.bean.po.MarketTopicExample;
import com.project2.bean.vo.CreateTopicVo;
import com.project2.bean.vo.ReadTopicInnerGoodsInfo;
import com.project2.bean.vo.ReadTopicVo;
import com.project2.mapper.MarketGoodsMapper;
import com.project2.mapper.MarketTopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper topicMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData<MarketTopic> getAllTopics(PageBo page, String title, String subtitle) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andDeletedEqualTo(false);

        topicExample.setOrderByClause(page.getSort() + " " + page.getOrder());

        if(title != null && !title.equals("")){
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && !subtitle.equals("")) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }

        List<MarketTopic> marketTopics = topicMapper.selectByExample(topicExample);

        PageInfo<MarketTopic> marketTopicPageInfo = new PageInfo<>(marketTopics);

        return CommonData.data(marketTopicPageInfo);
    }

    @Override
    public CreateTopicVo createTopic(MarketTopic newTopic) {
        //逻辑删除位设置为false
        newTopic.setDeleted(false);
        // 增加两个时间
        Date now = new Date();
        newTopic.setAddTime(now);
        newTopic.setUpdateTime(now);
        // 修改逆向工程的SQL语句，生成id并返回
        topicMapper.insertSelective(newTopic);

        CreateTopicVo createdTopic = new CreateTopicVo();

        createdTopic.setId(newTopic.getId());
        createdTopic.setTitle(newTopic.getTitle());
        createdTopic.setSubtitle(newTopic.getSubtitle());
        createdTopic.setPrice(newTopic.getPrice());
        createdTopic.setContent(newTopic.getContent());
        createdTopic.setReadCount(newTopic.getReadCount());
        createdTopic.setPicUrl(newTopic.getPicUrl());
        createdTopic.setAddTime(newTopic.getAddTime());
        createdTopic.setUpdateTime(newTopic.getUpdateTime());
        createdTopic.setGoods(newTopic.getGoods());

        return createdTopic;
    }

    @Override
    public ReadTopicVo getSpecifiedTopicInfo(Integer id) {
        // 先取到一个topic对象
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        // 再根据goods中的id，去抓取需要商品信息
        Integer[] goodsIds = topic.getGoods();

        List<ReadTopicInnerGoodsInfo> goodsInfoList = new ArrayList<>();

        if(goodsIds != null){
            for (Integer goodsId : goodsIds) {
                MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);

                ReadTopicInnerGoodsInfo info = new ReadTopicInnerGoodsInfo();

                info.setId(goods.getId());
                info.setName(goods.getName());
                info.setBrief(goods.getBrief());
                info.setCounterPrice(goods.getCounterPrice().doubleValue());
                info.setRetailPrice(goods.getRetailPrice().doubleValue());
                info.setIsHot(goods.getIsHot());
                info.setIsNew(goods.getIsNew());
                info.setPicUrl(goods.getPicUrl());

                goodsInfoList.add(info);
            }
        }

        ReadTopicVo Vo = new ReadTopicVo();
        Vo.setGoodsList(goodsInfoList);
        Vo.setTopic(topic);
        return Vo;
    }

    @Override
    public MarketTopic updateTopicInfo(MarketTopic updatedTopic) {
        updatedTopic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKeySelective(updatedTopic);
        return updatedTopic;
    }

    @Override
    public void deleteTopic(MarketTopic deleteInfo) {
        deleteInfo.setUpdateTime(new Date());
        deleteInfo.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(deleteInfo);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        MarketTopic updateInfo = new MarketTopic();
        Date now = new Date();
        updateInfo.setUpdateTime(now);
        updateInfo.setDeleted(true);

        MarketTopicExample topicExample = new MarketTopicExample();
        MarketTopicExample.Criteria topicExampleCriteria = topicExample.createCriteria();
        topicExampleCriteria.andIdIn(ids);

        topicMapper.updateByExampleSelective(updateInfo, topicExample);
    }
}
